﻿Imports System.Text.RegularExpressions

Public Class Formulario1

    'Private Sub MaskedTextBox1_MaskInputRejected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MaskInputRejectedEventArgs) Handles MaskedTextBox1.MaskInputRejected

    'End Sub

    'Private Sub MaskedTextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MaskedTextBox1.Validating
    '    MsgBox("hola")
    'End Sub

    Private Sub MaskedTextBox1_MaskInputRejected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MaskInputRejectedEventArgs) Handles MaskedTextBox1.MaskInputRejected

    End Sub

    Private Sub MaskedTextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MaskedTextBox1.Validating
        Dim email As String = MaskedTextBox1.Text
        If ComprobarEmail(email) = False Then

            Dim resultado As DialogResult _
            = MessageBox.Show("El email introducido no es válido." & _
                                       " ¿Desea reintroducirlo?", "Dirección de email no válida", _
                                       MessageBoxButtons.YesNo, MessageBoxIcon.Error)
            If resultado = Windows.Forms.DialogResult.Yes Then
                e.Cancel = True
            End If

        End If


    End Sub

    Function ComprobarEmail(ByVal emailAddress As String) As Boolean
        Dim pattern As String = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]" & _
        "*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
        Dim emailAddressMatch As Match = Regex.Match(emailAddress, pattern)
        If emailAddressMatch.Success Then
            ComprobarEmail = True
        Else
            ComprobarEmail = False
        End If
    End Function

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.Hide()
        Formulario2.Show()
    End Sub
End Class
